import {
  HTTP
} from '../../utils/http'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boxList: [], // 查看其他用户时数据
    boxPageNum: 1,
    boxPageSize: 10,
    boxTotal: 0,
    publicBoxList: [], // 公开数据
    publicBoxPageNum: 1,
    publicBoxPageSize: 10,
    publicBoxTotal: 0,
    privateBoxList: [], // 私密数据
    privateBoxPageNum: 1,
    privateBoxPageSize: 10,
    privateBoxTotal: 0,
    userInfo: {
      id: null
    },
    isMe: false, // 是否查看自己
    tabActiveTitle:"公开",
    optionsAccountId:null
  },

  getUserListData(userId, param, success) {
    let data = Object.assign({
      "accountId": userId,
      "type": 0
    }, param)
    http.request({
      url: '/ai/draw/box/get/account/page',
      method: 'post',
      data: data,
      success: (res) => {
        if (res.code === "0") {
          success && success(res)
        }
      }
    })
  },

  /**
   * 获取用户数据逻辑
   * 1.当userInfo登录的用户id与url参数传递的id一致时，表示为查看自己的数据，可以看公开和私密
   * 2.当userInfo登录的用户id与url参数传递的id不一致是，表示为查看其他用户的数据，仅查看公开数据
   */
  getListData(urlId) {
    if (urlId == this.data.userInfo.id) {
        this.getPublicList()
        this.getPrivateList()
    } else {
      this.getBoxList(urlId)
    }
  },

  getBoxList(urlId) {
    this.getUserListData(urlId, {
      isPrivate: 0,
      "pageNum": this.data.boxPageNum,
      "pageSize": this.data.boxPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.privateBoxList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        boxList: arr1,
        boxTotal: res.data.total
      })
    })
  },

  getPrivateList() {
    // 获取私密数据
    this.getUserListData(this.data.userInfo.id, {
      isPrivate: 1,
      "pageNum": this.data.privateBoxPageNum,
      "pageSize": this.data.privateBoxPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.privateBoxList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        privateBoxList: arr1,
        privateBoxTotal: res.data.total
      })
    })
  },

  getPublicList() {
    // 获取公开数据
    this.getUserListData(this.data.userInfo.id, {
      isPrivate: 0,
      "pageNum": this.data.publicBoxPageNum,
      "pageSize": this.data.publicBoxPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.publicBoxList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        publicBoxList: arr1,
        publicBoxTotal: res.data.total
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userId = wx.getStorageSync('userId')
    this.setData({
      "userInfo.id": userId,
      isMe: options.accountId == userId,
      "optionsAccountId":options.accountId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getListData(this.data.optionsAccountId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      boxList: [], // 查看其他用户时数据
      boxPageNum: 1,
      boxPageSize: 10,
      publicBoxList: [], // 公开数据
      publicBoxPageNum: 1,
      publicBoxPageSize: 10,
      privateBoxList: [], // 私密数据
      privateBoxPageNum: 1,
      privateBoxPageSize: 10
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  onChange(e){
    this.setData({
      tabActiveTitle: e.detail.title
    })
    if(e.detail.title === '公开') {
      this.setData({
        publicBoxList: [],
        publicBoxPageNum: 1,
        publicBoxPageSize: 10,
        publicBoxTotal: 0
      })
      this.getPublicList()
    }else {
      this.setData({
        privateBoxList: [],
        privateBoxPageNum: 1,
        privateBoxPageSize: 10,
        privateBoxTotal: 0
      })
      this.getPrivateList()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isMe) {
      if (this.data.tabActiveTitle === "公开") {
        if (!(this.data.publicBoxList && this.data.publicBoxList.length > 0)) return
        let currentTotal = this.data.publicBoxList.length;
        if (currentTotal <= this.data.publicBoxTotal) {
          var current = this.data.publicBoxPageNum * 1 + 1;
          this.setData({
            publicBoxPageNum: current
          })
          this.getPublicList();
        }
      } else if (this.data.tabActiveTitle === "私密") {
        if (!(this.data.privateBoxList && this.data.privateBoxList.length > 0)) return
        let currentTotal = this.data.privateBoxList.length;
        if (currentTotal <= this.data.privateBoxTotal) {
          var current = this.data.privateBoxPageNum * 1 + 1;
          this.setData({
            privateBoxPageNum: current
          })
          this.getPrivateList();
        }
      } else {
        if (!(this.data.boxList && this.data.boxList.length > 0)) return
        let currentTotal = this.data.boxList.length;
        if (currentTotal <= this.data.boxTotal) {
          var current = this.data.boxPageNum * 1 + 1;
          this.setData({
            boxPageNum: current
          })
          this.getBoxList();
        }
      }

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})